package com.android.gufengmall.admin;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import com.android.gufengmall.BaseActivity;
import com.android.gufengmall.R;
import com.android.gufengmall.utils.LogUtils;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

public class AdminActivity extends BaseActivity {

    private static final String TAG = "AdminActivity";

    private ViewPager2 mViewPager2;
    private TabLayout mTabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admin);

        mViewPager2 = findViewById(R.id.vp2);
        mTabLayout = findViewById(R.id.tab_layout);

        mViewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                if (position == 0) {
                    return UserManagerFragment.newInstance();
                } else if (position == 1) {
                    return ArticleManagerFragment.newInstance();
                } else if (position == 2) {
                    return MallManagerFragment.newInstance();
                } else {
                    return VideoManagerFragment.newInstance();
                }
            }

            @Override
            public int getItemCount() {
                return 4;
            }
        });

        new TabLayoutMediator(mTabLayout, mViewPager2, (tab, position) -> {
            LogUtils.d(TAG, "position-->" + position);
            if (position == 0) {
                tab.setText("用户管理");
            } else if (position == 1) {
                tab.setText("文章管理");
            } else if (position == 2) {
                tab.setText("商品管理");
            } else {
                tab.setText("视频管理");
            }
        }).attach();
    }
}